package com.xiaoyu.lanling.base.push.alternate;

import android.content.Context;

import com.xiaomi.mipush.sdk.MiPushClient;
import com.xiaoyu.base.AppConfig;
import com.xiaoyu.base.push.PushConstants;

import in.srain.cube.util.DeviceInfoUtil;


public class PushAlternateAgent {

    private static final PushAlternateAgent ourInstance = new PushAlternateAgent();

    private OnPushReceiver mOnPushReceiver;
    private String mPushType = PushConstants.PUSH_TYPE_XIAOMI;
    private String mPushToken = "";

    public static PushAlternateAgent getInstance() {
        return ourInstance;
    }

    private PushAlternateAgent() {
    }

    public void startPushServer(Context context) {
        if (context == null) {
            return;
        }
        if (DeviceInfoUtil.isHasPermission(context, "android.permission.READ_PHONE_STATE")
                || DeviceInfoUtil.isHasPermission(context, "android.permission.WRITE_EXTERNAL_STORAGE")) {
            MiPushClient.registerPush(context, AppConfig.miAppId(), AppConfig.miAppKey());
        } else {
            onPushError("need permissions");
        }
    }

    public void onPushToken(String token) {
        mPushToken = token;
        if (mOnPushReceiver != null) {
            mOnPushReceiver.onPushToken(mPushType, mPushToken);
        }
    }

    public void onPushError(String errorMessage) {
        if (mOnPushReceiver != null) {
            mOnPushReceiver.onPushError(errorMessage);
        }
    }

    public void setOnPushReceiver(OnPushReceiver onPushReceiver) {
        mOnPushReceiver = onPushReceiver;
    }

    public interface OnPushReceiver {

        void onPushToken(String type, String token);

        void onPushError(String errorMessage);
    }
}
